var express = require('express');
var router = express.Router();
var http = require("http");
var async = require("async");
var MongoClient = require("mongodb").MongoClient;
var DB_CONN_STR = "mongodb://127.0.0.1:27017/maizuo";

router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

// 获取轮播图数据		localhost:8080/carousel
router.get('/carousel', function(req, res, next) {
	// 获取当前时间用于拼接URL地址
	var time = (new Date()).getTime();
  	http.get("http://m.maizuo.com/v4/api/billboard/home?__t="+time,function(response){
  		var html = "";
  		response.on("data",function(chunk){
  			html+=chunk;
  		})

  		response.on("end",function(){
  			res.send(html);
  		})
  	})
});

// 获取热映电影部分数据		localhost:8080/home.section1?page=1&count=2
router.get('/home_section1', function(req, res, next) {
	// var page = req.jquery.page;
	// var count = req.jquery.count;
	// 获取当前时间用于拼接URL地址
	var time = (new Date()).getTime();
	// "http://m.maizuo.com/v4/api/film/now-playing?__t="+time+"&page="+page+"&count="+count
  	http.get("http://m.maizuo.com/v4/api/film/now-playing?__t="+time,function(response){
  		var html = "";
  		response.on("data",function(chunk){
  			html+=chunk;
  		})

  		response.on("end",function(){
  			res.send(html);
  		})
  	})
});

// 获取即将热映电影部分数据		localhost:8080/home.section2?page=1&count=2
router.get('/home_section2', function(req, res, next) {
	// var page = req.jquery.page;
	// var count = req.jquery.count;
	// 获取当前时间用于拼接URL地址
	var time = (new Date()).getTime();
	// "http://m.maizuo.com/v4/api/film/now-playing?__t="+time+"&page="+page+"&count="+count
  	http.get("http://m.maizuo.com/v4/api/film/coming-soon?__t="+time,function(response){
  		var html = "";
  		response.on("data",function(chunk){
  			html+=chunk;
  		})
  		response.on("end",function(){
  			res.send(html);
  		})
  	})
});

// 获取热映电影部分数据   localhost:8080/now_playing?page=1&count=2
router.get('/now_playing', function(req, res, next) {
    var page = req.query.page;
    var count = req.query.count;
    //   http://m.maizuo.com/v4/api/film/now-playing?page=1&count=7
    http.get("http://m.maizuo.com/v4/api/film/now-playing?page="+page+"&count="+count,function(response){
      var html = "";
      response.on("data",function(chunk){
        html+=chunk;
      })
      response.on("end",function(){
        res.send(html);
      })
    })
});

// 获取即将上映电影部分数据   localhost:8080/will_playing?page=1&count=2
router.get('/will_playing', function(req, res, next) {
  // var page = req.jquery.page;
  // var count = req.jquery.count;
    //   http://m.maizuo.com/v4/api/film/coming-soon?page=1&count=7
    http.get("http://m.maizuo.com/v4/api/film/coming-soon?",function(response){
      var html = "";
      response.on("data",function(chunk){
        html+=chunk;
      })
      response.on("end",function(){
        res.send(html);
      })
    })
});

// 获取影院部分数据   localhost:8080/cinema
router.get('/cinema', function(req, res, next) {
  // var page = req.jquery.page;
  // var count = req.jquery.count;
  // 获取当前时间用于拼接URL地址
  var time = (new Date()).getTime();
    //   http://m.maizuo.com/v4/api/cinema?__t=1500437535261
    http.get("http://m.maizuo.com/v4/api/cinema?__t="+time,function(response){
      var html = "";
      response.on("data",function(chunk){
        html+=chunk;
      })
      response.on("end",function(){
        res.send(html);
      })
    })
});

// 获取详情页面部分数据   localhost:8080/detail
router.get('/detail', function(req, res, next) {
  // 获取当前时间用于拼接URL地址
  var num = req.query.id;
  console.log(num);
  var time = (new Date()).getTime();
    //   http://m.maizuo.com/v4/api/film/3756?__t=1500455569678
    http.get("http://m.maizuo.com/v4/api/film/"+num+"?__t="+time,function(response){
      var html = "";
      response.on("data",function(chunk){
        html+=chunk;
      })
      response.on("end",function(){
        res.send(html);
      })
    })
});

// 获取登录路由   localhost:8080/myPage
router.get('/myPage', function(req, res, next) {
    var user = req.query.user;
    var pass = req.query.pass;
    MongoClient.connect(DB_CONN_STR,function(err,db){
        var conn = db.collection("project");

        async.series([
          function(cb){
              conn.find({name:user}).count(function(err,num){
                if(err){
                    cb("err",{msg:-1,code:"用户名查询失败"});        //用户名查询失败
                }else{
                    if(num>0){      //用户名存在
                        cb(null,{msg:0,code:"用户名存在"});
                    }else{          //用户名存在,直接保存到数据库
                        conn.save({name:user,pass:pass},function(err,data){
                            if(err){
                                cb("err",{msg:-1,code:"数据保存失败"})
                            }else{
                                cb("err",{msg:1,code:"数据保存成功"});
                            }
                        })
                    }
                }
              })
          },
          function(cb){     //顺便判断登录密码是否信息一致
                conn.find({name:user,pass:pass}).count(function(err,count){
                    if(err){
                         cb('err',{msg:-1,code:"用户名及密码查询失败"});
                    }else{
                        if(count>0){        //查到证明用户名及密码输入正确
                            cb(null,{msg:1,code:"登录用户名及密码正确",userName:user})
                        }else{              //反之密码输入错误
                            cb("err",{msg:-1,code:"该账号已存在，但密码输入错误"})
                        }
                    }
                })
          }
        ],function(err,result){
            if(err){
                res.send(result);
            }else{
                res.send(result);
            }
        })
    })
          
});
module.exports = router;
